package com.pdstudio.weather;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pdstudio.app.Application;
import com.pdstudio.bean.WeatherActual;
import com.pdstudio.bean.WeatherDay;
import com.pdstudio.bean.WeatherProfile;
import com.pdstudio.bean.WeatherStamp;
import com.pdstudio.http.SettingHttpMethod;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.util.UpdateManager;
import com.pdstudio.view.SwipeBackActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserAuthActivity extends SwipeBackActivity implements
		Application.EventHandler {

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	private EditText tvAuthCode;
	private Button btnSaveAuthCode;

	private SharePreferenceUtil mSpUtil;
	private Application mApplication;
	private Handler handler = null;
	private Integer isAuth = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_auth);

		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					try {
						Toast.makeText(getApplicationContext(), "验证中...", 0)
								.show();
						// msg.obj是获取handler发送信息传来的数据
						@SuppressWarnings("unchecked")
						List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

						if (msg != null && msg.obj != null) {
							isAuth = (Integer) msg.obj;
						}
						setAuth();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		};

		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		if (true) {// TODO 这里判断天气情况
			LinearLayout layout = (LinearLayout) findViewById(R.id.activity_userauthcode);
			layout.setBackground(getResources().getDrawable(R.drawable.bgsun));
		}

		customLiveIndexTitleView = findViewById(R.id.title_userauthcode);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("授权码");

		tvAuthCode = (EditText) findViewById(R.id.tv_authCode);
		tvAuthCode.setText(mSpUtil.getAuthCode());
		btnSaveAuthCode = (Button) findViewById(R.id.btn_saveAuthCode);
		btnSaveAuthCode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (tvAuthCode.getText().toString().equals("")) {
					Toast.makeText(UserAuthActivity.this, "请输入授权码！",
							Toast.LENGTH_SHORT).show();
					return;
				}
				// 在这里判断授权码是否授权成功，授权成功则把授权信息保存到shareprefrences中。
				// 去接口获取版本号
				try {
					Thread thread = new Thread() {
						@Override
						public void run() {
							SettingHttpMethod whm = new SettingHttpMethod(
									getApplicationContext());
							Integer result = whm.IsAuth(tvAuthCode.getText()
									.toString());
							Message m = handler.obtainMessage(1, result);
							handler.sendMessage(m);
						}
					};
					thread.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

		ivBack = (ImageView) findViewById(R.id.title2_back);
		if (mSpUtil.getAuthCode().equals("")) {
			ivBack.setEnabled(false);
			ivBack.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Toast.makeText(UserAuthActivity.this, "请输入授权码！",
							Toast.LENGTH_SHORT).show();

				}
			});
		} else {

			ivBack.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					UserAuthActivity.this.finish();
				}
			});
		}

	}

	private void setAuth() {
		if (isAuth==1) {

			// TODO Auto-generated method stub
			mSpUtil.setAuthCode(tvAuthCode.getText().toString());
			// 使用Toast信息提示
			Toast.makeText(UserAuthActivity.this,
					mSpUtil.getAuthCode() + "授权成功", Toast.LENGTH_SHORT).show();
			UserAuthActivity.this.finish();

		} else {
			Toast.makeText(UserAuthActivity.this,
					tvAuthCode.getText().toString() + "授权失败，请输入正确的授权码或联系管理员！",
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_auth, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}
}
